/*
 * Board.cpp
 *
 *  Created on: Mar 24, 2011
 *      Author: Craig Wilson
 */

#include "../inc/Board.h"


	Board::Board():moveChecker(this), stateChecker(this,&moveChecker){
		for (int i=0;i<BOARDSIZE;i++)
			for (int j=0;i<BOARDSIZE;j++)
				board[i][j]=Square(Position(i,j));
		reset();
	}

	Board::~Board(){

	}

	void Board::movePiece(Position src, Position dst){
		if (turn==WHITE)
			turn=BLACK;
		else
			turn=WHITE;
		Piece *moved,*captured;
		moved=getPiece(src);
		captured=getPiece(dst);

		board[src.getRow()][src.getColumn()].setPiece(NULL);	//change square's pointers to piece
		board[src.getRow()][src.getColumn()].setPiece(moved);

		moved->setPos(dst);	//Change moved piece's position

		if(captured!=NULL)
			captured->kill();

		history.addMove(Move(src,dst,moved,captured));
	}

	Piece* Board::getPiece(Position src){
		return board[src.getRow()][src.getColumn()].getPiece();
	}

	bool Board::undo(){
		if (!history.isEmpty()){
			Move undidMove = history.undoMove();
			movePiece(undidMove.getDest(),undidMove.getSource());
			if (undidMove.getCapturedPiece()!=NULL){
				undidMove.getCapturedPiece()->unKill();
				board[undidMove.getDest().getColumn()][undidMove.getDest().getRow()].setPiece(undidMove.getCapturedPiece());
			}
			return true;
		}
		return false;
	}

	vector<Position> Board::getMoves(Position src){
		return moveChecker.getValidMoves(src);
	}

	BoardCondition Board::getCondition(Color player){
		return stateChecker.getState(player);
	}

	Color Board::currentTurn(){
		return turn;
	}

	void Board::reset(){
		history.reset();
		//white pieces
		for(int i=0; i<BOARDSIZE; i++)
			whitePieces.push_back(new Pawn(WHITE,Position(i,BOARDSIZE-2)));
		whitePieces.push_back(new Rook(WHITE,Position(0,BOARDSIZE-1)));
		whitePieces.push_back(new Rook(WHITE,Position(7,BOARDSIZE-1)));
		whitePieces.push_back(new Knight(WHITE,Position(1,BOARDSIZE-1)));
		whitePieces.push_back(new Knight(WHITE,Position(6,BOARDSIZE-1)));
		whitePieces.push_back(new Bishop(WHITE,Position(2,BOARDSIZE-1)));
		whitePieces.push_back(new Bishop(WHITE,Position(5,BOARDSIZE-1)));
		whitePieces.push_back(new Queen(WHITE,Position(3,BOARDSIZE-1)));
		whitePieces.push_back(new King(WHITE,Position(4,BOARDSIZE-1)));

		//blackpieces
		for(int i=0; i<BOARDSIZE;i++)
			blackPieces.push_back(new Pawn(BLACK,Position(i,1)));
		blackPieces.push_back(new Rook(BLACK,Position(0,0)));
		blackPieces.push_back(new Rook(BLACK,Position(7,0)));
		blackPieces.push_back(new Knight(BLACK,Position(1,0)));
		blackPieces.push_back(new Knight(BLACK,Position(6,0)));
		blackPieces.push_back(new Bishop(BLACK,Position(2,0)));
		blackPieces.push_back(new Bishop(BLACK,Position(5,0)));
		blackPieces.push_back(new Queen(BLACK,Position(4,0)));
		blackPieces.push_back(new King(BLACK,Position(3,0)));

	}

	MoveHistory	Board::getMoveHistory(){
		return history;
	}

	void Board::loadHistory(MoveHistory &reverse){
		reset();
		while (!reverse.isEmpty()){
			Move temp= reverse.undoMove();
			movePiece(temp.getSource(), temp.getDest());
		}
	}




